package com.weilele.mvvm.utils.`object`

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.BitmapDrawable
import android.media.RingtoneManager
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import android.annotation.SuppressLint


/**
 * 描述：发送通知栏的使用案列
 */
object NotifyObj {
    fun sendNotifyActivity(
            context: Context/*上下文*/,
            intent: Intent/*跳转的activity*/,
            title: String?/*标题*/,
            msg: String?/*内容*/,
            NOTIFICATION_ID: Int/*通知id*/,
            name: CharSequence/*用户可见频道名称，8.0以上有效*/,
            smallIcon: Int /*小图标，纯色*/,
            bigIcon: Int/*左边的logo*/,
            @SuppressLint("InlinedApi") priority: Int = NotificationManager.IMPORTANCE_MAX/*优先级，8.0以上有效*/
    ) {
        val pendingIntent = PendingIntent.getActivity(
            context, 0,
            intent, PendingIntent.FLAG_ONE_SHOT
        )
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(context, NOTIFICATION_ID.toString())
            .setSmallIcon(smallIcon)
            .setContentTitle(title)
            //使用默认的声音、振动、闪光
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentText(msg)
            .setLargeIcon((ContextCompat.getDrawable(context, bigIcon) as BitmapDrawable).bitmap)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setShowWhen(true)
            .setTicker("$title-$msg")//在状态栏短暂显示的文字
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(NOTIFICATION_ID.toString(), name, priority)
            notificationManager.createNotificationChannel(channel)
        }
        notificationManager.notify(javaClass.simpleName, NOTIFICATION_ID, notificationBuilder.build())
    }

    fun clearNotify(context: Context, id: Int) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(id)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.deleteNotificationChannel(id.toString())
        }
    }
}